//
//  PersonalCenterVC.m
//  EPTool
//
//  Created by guoshiqing on 2018/4/19.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "PersonalCenterVC.h"
#import "UserSettings.h"
#import "BottomTopLineTableViewCell.h"
#import "SettingVC.h"

#define PERCENT_HEAD_HEIGHT 136.f
#define PERCENT_HEAD_TOTAL PERCENT_HEAD_HEIGHT+NavBar_Height

#define PC_MyCer @"我的认证"
#define PC_MyRes @"我的预约"
#define PC_Clt   @"我的收藏"
#define PC_Pay   @"付费购买"
#define PC_QR    @"扫一扫"
#define PC_Rcmd  @"推荐给朋友"
#define PC_Sett  @"设置"
#define PC_Serv  @"联系客服"
#define PC_H5Test  @"h5测试"
#define PC_PushTest @"推送测试"

#define PhotoStr @"+852 5515 2000"
#define EmailStr @"service@insucrm.com"

@interface PersonalCenterVC() <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *menuList;
@property (nonatomic, strong) NSArray *iconList;
@end

@implementation PersonalCenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self hideBackButton];
    self.view.backgroundColor = [EPColor epColor_Bg];
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    // 取消所有本地通知
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}


#pragma mark - PersonalCenterViewDelegate
// 点击头像
- (void)headIconAction
{
}

// 点击我的认证
- (void)myCertificationAction
{
}

// 点击我的预约
- (void)myReservationAction
{
}

// 点击我的收藏
- (void)myCollectAction
{
}

// 点击付费购买
- (void)paidPurchaseAction
{
}

// 扫一扫
- (void)qrCodeAction
{
}


#pragma mark - Action
// 点击推荐给好友
- (void)recommendAction
{
}

// 点击设置
- (void)settingAction
{
    SettingVC *settingVC = [[SettingVC alloc] init];
    [self.navigationController pushViewController:settingVC animated:YES];
}

// 点击h5测试
- (void)h5TestAction
{
}

// 联系客服
- (void)contactService
{
}

- (void)phoneActivity
{
}

- (void)emailActivity
{
}

// 推送测试
- (void)pushTestAction
{
    // 1.创建一个本地通知
    UILocalNotification *localNote = [[UILocalNotification alloc] init];
    
    // 1.1.设置通知发出的时间
    localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
    
    // 1.2.设置通知内容
    localNote.alertBody = @"这是一个推送这是一个推送";
    
    // 1.3.设置锁屏时,字体下方显示的一个文字
    localNote.alertAction = @"赶紧!!!!!";
    localNote.hasAction = YES;
    
    // 1.4.设置启动图片(通过通知打开的)
    localNote.alertLaunchImage = @"";
    
    // 1.5.设置通过到来的声音
    localNote.soundName = UILocalNotificationDefaultSoundName;
    
    // 1.6.设置应用图标左上角显示的数字
    localNote.applicationIconBadgeNumber = 0;
    
    // 1.7.设置一些额外的信息
    //    [module isEqualToString:@"hotelReserveNotice"] ||
    //    [module isEqualToString:@"medicalReserveNotice"] ||
    //    [module isEqualToString:@"tradeCarReserveNotice"]
    localNote.userInfo = @{@"pushType" : @"medicalReserveStatus", @"status":@(3)};
    
    // 2.执行通知
    UIUserNotificationSettings *localSet = [UIUserNotificationSettings settingsForTypes:1 << 2 categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:localSet];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNote];
}

//////////////////////////////////////////////////////
#pragma mark - 懒加载
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_CONTENT_HEIGHT+20) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellEditingStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

// 此处不用@[@{@"key":"value"}]形式是以为位置会变化
- (NSArray *)menuList
{
    if (!_menuList) {
        
        if ([UserSaveInfo.mobile isEqualToString:@"15088132123"] || [UserSaveInfo.mobile isEqualToString:@"15088132122"]) {
            _menuList = @[@[@" "], @[PC_MyCer], @[PC_Rcmd, PC_Serv], @[PC_Sett]];
        }
        else if ([UserSaveInfo.mobile rangeOfString:@"1508813"].location != NSNotFound || [UserSaveInfo.mobile isEqualToString:@"17520121111"])
        {
            _menuList = @[@[@" "], @[PC_MyCer, PC_MyRes, PC_Clt], @[PC_Pay, PC_QR, PC_Rcmd, PC_Serv, PC_H5Test, PC_PushTest], @[PC_Sett]];
        }
        else
        {
            _menuList = @[@[@" "], @[PC_MyCer, PC_MyRes, PC_Clt], @[PC_Pay, PC_QR, PC_Rcmd, PC_Serv], @[PC_Sett]];
        }
    }
    return _menuList;
}

- (NSArray *)iconList
{
    if (!_iconList) {
        
        if ([UserSaveInfo.mobile isEqualToString:@"15088132123"] || [UserSaveInfo.mobile isEqualToString:@"15088132122"]) {
            _iconList = @[@[@" "], @[@"me_icon_authentication"], @[@"me_icon_Invitation", @"me_icon_serv"], @[@"me_icon_set"]];
        }
        else if ([UserSaveInfo.mobile rangeOfString:@"1508813"].location != NSNotFound || [UserSaveInfo.mobile isEqualToString:@"17520121111"])
        {
            _iconList = @[@[@" "], @[@"me_icon_authentication", @"me_icon_bespeak", @"me_icon_collection"], @[@"me_icon_payfor", @"me_icon_qrcode", @"me_icon_Invitation", @"me_icon_serv", @"me_icon_set", @"me_icon_set"], @[@"me_icon_set"]];
        }
        else
        {
            _iconList = @[@[@" "], @[@"me_icon_authentication", @"me_icon_bespeak", @"me_icon_collection"], @[@"me_icon_payfor", @"me_icon_qrcode", @"me_icon_Invitation", @"me_icon_serv"], @[@"me_icon_set"]];
        }
    }
    return _iconList;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self.menuList objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 头部
    WEAKSELF
    if (0 == indexPath.section) {
    }
    
    // 其他部分
    static NSString *identifier = @"PersonalCenterViewController";
    BottomTopLineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[BottomTopLineTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    NSArray *nemus = [self.menuList objectAtIndex:indexPath.section];
    NSArray *icons = [self.iconList objectAtIndex:indexPath.section];
    
    if (indexPath.row + 1 != nemus.count) {
        cell.bottomLineLeftEdge = Cell_X;
    }
    else
    {
        cell.bottomLineHidden = YES;
    }
    cell.imageView.image = [UIImage imageNamed:[icons objectAtIndex:indexPath.row]];
    cell.textLabel.text = [nemus objectAtIndex:indexPath.row];
    cell.textLabel.textColor = [EPColor epColor_TitleBlack];
    cell.textLabel.font = SystemFont(16.f);
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.menuList count];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return Cell_Spacing_H;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (0 == indexPath.section) {
        return PERCENT_HEAD_TOTAL;
    }
    return 56.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSArray *subList = [self.menuList objectAtIndex:indexPath.section];
    NSString *strSelected = [subList objectAtIndex:indexPath.row];
    if ([strSelected isEqualToString:PC_MyCer])
    {
        // 跳转我的认证
        [self myCertificationAction];
    }
    else if ([strSelected isEqualToString:PC_MyRes])
    {
        // 跳转我的预约
        [self myReservationAction];
    }
    else if ([strSelected isEqualToString:PC_Clt])
    {
        // 跳转我的收藏
        [self myCollectAction];
    }
    else if ([strSelected isEqualToString:PC_Pay])
    {
        // 跳转付费购买
        [self paidPurchaseAction];
    }
    else if ([strSelected isEqualToString:PC_QR])
    {
        // 扫一扫
        [self qrCodeAction];
    }
    else if ([strSelected isEqualToString:PC_Rcmd])
    {
        // 推荐动能保险助手给朋友
        [self recommendAction];
    }
    else if ([strSelected isEqualToString:PC_Sett])
    {
        // 设置
        [self settingAction];
    }
    else if ([strSelected isEqualToString:PC_H5Test])
    {
        // h5页面暂时集合
        [self h5TestAction];
    }
    if ([strSelected isEqualToString:PC_PushTest])
    {
        // 推送测试
        [self pushTestAction];
    }
    else if ([strSelected isEqualToString:PC_Serv])
    {
        // 联系客服
        [self contactService];
    }
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, Cell_Spacing_H)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

@end
